package com.wms.web.action;

import com.wms.model.User;
import com.wms.result.ResultMap;
import com.wms.service.UserService;
import com.wms.utils.MD5Utils;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Timestamp;

/**
 * @author This
 * @version 1.0
 * @date 2019/7/10 11:45
 */

public class UserAction extends BaseAction<User> {

    @Autowired
    private UserService service;

    /**
     * 判断用户是否除admin用户还有其他用户，有则返回true，否则返回false
     * 若用户表数据为空，则插入admin用户，密码为123456
     *
     * @throws IOException io异常
     */
    public void hasUser() throws IOException {
        boolean b = service.hasUser();
        if (service.count(null) < 1) {
            User user = new User("admin", MD5Utils.text2md5("123456"), (byte) 0, new Timestamp(System.currentTimeMillis()));
            service.add(user);
        }
        responseStr("{\"flag\":" + b + "}");
    }

    /**
     * 注册用户
     */
    public void register() throws IOException {
        if (service.hasUser()) {
            responseJson(new ResultMap(0, "请求错误！"), new String[]{"data"});
            return;
        }
        User model = getModel();
        model.setPassword(MD5Utils.text2md5(model.getPassword()));
        model.setRole((byte) 0);
        model.setTime(new Timestamp(System.currentTimeMillis()));
        if (service.add(model)) {
            responseJson(new ResultMap(1, "注册成功，赶紧登录吧！"), new String[]{"data"});
            return;
        }
        responseJson(new ResultMap(0, "用户名已存在！"), new String[]{"data"});
    }

    /**
     * 登录操作
     */
    public void login() throws IOException {
        String username = getModel().getUsername();
        String password = getModel().getPassword();
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            responseJson(new ResultMap(0, "用户名或密码不能为空！"), new String[]{"data"});
            return;
        }
        User user = service.getOne(username, MD5Utils.text2md5(password));
        if (user == null) {
            responseJson(new ResultMap(0, "用户名或密码错误！"), new String[]{"data"});
            return;
        }
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        session.setAttribute("loginUser", user);
        responseJson(new ResultMap(1, request.getContextPath() + "/page_index"), new String[]{"data"});
    }

    /**
     * 退出登录
     *
     * @return 登录页面
     */
    public String logout() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        session.removeAttribute("loginUser");
        return "login";
    }

    @Override
    public void save() {

    }

    @Override
    public void edit() {
        String username = getModel().getUsername();
        String password = getModel().getPassword();
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        User loginUser = (User) session.getAttribute("loginUser");
        int id = loginUser.getId();
//        service;
    }

    @Override
    public void delete() throws IOException {

    }

    @Override
    public void showList() {

    }


    public void test() throws IOException {
        responseStr("<?xml version=\"1.0\" encoding=\"UTF-8\"?><users><name>Alexia</name><age>23</age><sex>Female</sex></users>");
    }
}
